<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            margin-left: 200px;
            margin-top: 200px;
        }
    </style>
</head>

<body>
    <div></div>

    <script>
        var box = document.querySelector('div');
        box.addEventListener('mousemove', function (e) {
            // console.log(e.pageX, e.pageY);
            //e.pageX, e.pageY 鼠标在页面中的坐标
            // console.log(box.offsetLeft, box.offsetHeight);
            //盒子在页面中的距离

            //下面的是鼠标在盒子里面的坐标，相减即可
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            console.log(x, y);

        })
    </script>
</body>

</html>